package cn.com.org.dao;

import java.util.List;

import org.bson.Document;
import org.bson.conversions.Bson;
import org.bson.types.ObjectId;

import com.google.gson.Gson;
import com.mongodb.BasicDBObject;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import com.mongodb.client.result.DeleteResult;

public class MongoUtil {
	private String dbname;
	private String dbtable;
	private MongoDatabase mongodbBase;
	private MongoCollection<Document> mongoCollection;
	public String getDbname() {
		return dbname;
	}
	public void setDbname(String dbname) {
		this.dbname = dbname;
	}
	public String getDbtable() {
		return dbtable;
	}
	public void setDbtable(String dbtable) {
		this.dbtable = dbtable;
	}

	public MongoUtil(String dbname,String dbtable){
		this.dbname=dbname;
		this.dbtable=dbtable;
		this.mongoCollection=MongoBase.getDB(dbname).getCollection(dbtable);
	}

	public Document findByObjectid(String objectid){
		if(objectid==null||!ObjectId.isValid(objectid)){
			return null;
		}
		ObjectId objectId=new ObjectId(objectid);
		Document document=findOne("_id",objectId);
		return document;
	}
	public Document findOne(String key,Object value){
		Document document=find(key,value).first();
		Document documentaa=find(Filters.eq(key, value)).first();
		return document;
	}
	public FindIterable<Document> find(String key,Object value){
		if(key==null||key.isEmpty()){
			return null;
		}
		BasicDBObject query = new BasicDBObject();		
		query.put(key, value);
		FindIterable<Document> documents=find(query);
		return documents;
	}
	public  FindIterable<Document> find(Bson bson){
		FindIterable<Document> documents=mongoCollection.find(bson);
		return documents;
	}
	public FindIterable<Document> find(){
		FindIterable<Document> documents=mongoCollection.find();
		
		return documents;
	}
	public MongoCursor<Document> findByPage(MongoCollection<Document> coll, Bson filter, int pageNo, int pageSize) {
	     Bson orderBy = new BasicDBObject("_id", 1);
	    return coll.find(filter).sort(orderBy).skip((pageNo - 1) * pageSize).limit(pageSize).iterator();
	}
	public long count(){
        long count=	mongoCollection.count();
        return count;
	}
	public long count(String key,Object value){
		if(key==null||key.isEmpty()){
			return 0;
		}
		BasicDBObject queryone = new BasicDBObject();		
		queryone.put(key, value);
		return  count(queryone);
	}
	public long count(Bson bson){
		return mongoCollection.count(bson);
	}
	
	public boolean insertOne(Document document){
		if(document==null){
			return false;
		}
		mongoCollection.insertOne(document);
		return true;
	}
	public boolean insertOne(String json){
		if(json==null||json.isEmpty()){
			return false;
		}
		  Document  document= Document.parse(json);
		  return  insertOne(document);
	}
	public <T> Boolean InsertOne(T entity){
		if(entity==null){
			return false;
		}
		Gson gson=new Gson();
        String json=gson.toJson(entity);
        return  insertOne(json);
	}
	public boolean insertMany(List<? extends Document> args){
		if(args==null){
			return false;
		}
		mongoCollection.insertMany(args);
		return true;
	}
	public long deleteOne(Bson bson){
		if (bson==null) {
			return 0;
		}
	  DeleteResult deResult=mongoCollection.deleteOne(bson);
	  return  deResult.getDeletedCount();
	}
	public long deleteOne(String key,Object value){
		if(key==null||key.isEmpty()){
			return 0;
		}
		return deleteOne(Filters.eq("key", value));
	}
	public long deleteMany(Bson bson){
		if(bson==null){
			return 0;
		}
		return mongoCollection.deleteMany(bson).getDeletedCount();
	}
	public long deleteMany(String key,Object value){
		if(key==null||key.isEmpty()){
			return 0;
		}
		return deleteMany(Filters.eq(key, value));
	}
	public long updateOne(Bson source,Bson destination){
		if(source==null||destination==null){
			return 0;
		}
	   return	mongoCollection.updateOne(source, destination).getModifiedCount();
	}
	public long updateOne(String key,Object value,Object newValue){
		if(key==null||key.isEmpty()){
			return 0;
		}
		return updateOne(key, value, new Document("$set",new Document(key,newValue)));		
	}
	public long updateOne(String key,Object value,Bson bson){
		if(key==null||key.isEmpty()){
			return 0;
		}
		return updateOne(Filters.eq(key, value), bson);
	}
	public long updateOneById(Object idValue,String desKey,Object desValue){
		if(idValue==null){
			return 0;
		}
		return updateOneById(idValue,new Document("$set",new Document(desKey,desValue)));
	}
	public long updateOneById(Object idValue,Bson bson){
		if(idValue==null){
			return 0;
		}
		return updateOne("id", idValue, bson);
	}
	public long updateMany(String key,Object value,Object  desValue){
		if(key==null||key.isEmpty()){
			return 0;
		}
	   return	updateMany(Filters.eq(key,value), new Document(key,new Document("$set",desValue)));
	}
	public long updateMany(Bson source,Bson des){
		if(source==null||des==null){
			return 0;
		}
		return mongoCollection.updateMany(source, des).getModifiedCount();
	}
}
